﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MVCDemo.Models;


namespace MVCDemo.Controllers
{
    public class ShopController : Controller
    {
        // GET: Shop
        public ActionResult Index()
        {
            //查询
            //实例化上下文实例
            MemberModel db = new MemberModel();
            var Shops = db.Shops.ToList();
            return View(Shops);
        }

        // GET: Shop/Details/5 详情
        public ActionResult Details(int id)
        {
            MemberModel db = new MemberModel();
            var Shops = db.Shops.FirstOrDefault(u => u.S_ID == id);
            return View(Shops);
        }

        // GET: Shop/Create  跳转到添加的界面
        public ActionResult Create()
        {
            return View();
        }

        // POST: Shop/Create  添加具体执行
        [HttpPost]
        public ActionResult Create(Shops sModel)
        {
            try
            {

                // TODO: Add insert logic here
                MemberModel db = new MemberModel();
                db.Shops.Add(sModel);
                if (db.SaveChanges() > 0)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

        // GET: Shop/Edit/5 跳转编辑页面
        public ActionResult Edit(int id)
        {
            MemberModel db = new MemberModel();
            var Smodel = db.Shops.FirstOrDefault(u => u.S_ID == id);
            return View(Smodel);
        }

        // POST: Shop/Edit/5 编辑
        [HttpPost]
        public ActionResult Edit(Shops smodel)
        {
            try
            {
                // TODO: Add update logic here
                MemberModel db = new MemberModel();
                var smodels = db.Shops.FirstOrDefault(u => u.S_ID == smodel.S_ID);
                smodels.S_Name = smodel.S_Name;
                smodels.S_Category = smodel.S_Category;
                smodels.S_ContactName = smodel.S_ContactName;
                smodels.S_ContactTel = smodel.S_ContactTel;
                smodels.S_Address = smodel.S_Address;
                smodels.S_Remark = smodel.S_Remark;
                smodels.S_IsHasSetAdmin = smodel.S_IsHasSetAdmin;
                smodels.S_CreateTime = smodel.S_CreateTime;
                if (db.SaveChanges() > 0)
                {
                    return RedirectToAction("Index");
                }
                else
                {
                    return View();
                }
            }
            catch
            {
                return View();
            }
        }

        // GET: Shop/Delete/5
        public ActionResult Delete(int id)
        {
            MemberModel db = new MemberModel();
            var smodel = db.Shops.FirstOrDefault(u=>u.S_ID==id);
            return View(smodel);
        }

        // POST: Shop/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
                MemberModel db = new MemberModel();
                var smodel = db.Shops.FirstOrDefault(u => u.S_ID == id);
                if (smodel != null)
                {
                    db.Shops.Remove(smodel);
                    if (db.SaveChanges() > 0)
                    {
                        return RedirectToAction("Index");
                    }
                    else
                    {
                        return View();
                    }
                }
                else
                {
                    return View();
                }
               
            }
            catch
            {
                return View();
            }
        }
    }
}
